package com.itheima.service;

import com.itheima.entity.Brand;
import com.itheima.mapper.BrandsMapper;
import com.itheima.utils.SqlSessionFactoryutils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandService {
    //1.创建工厂对象,只做一次
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryutils.getSqlSessionFactory();
    public List<Brand> selectAll() {

       //2.获取sqlSession对象
        SqlSession a=sqlSessionFactory.openSession();
        //3.获取BrandMapper
        BrandsMapper mapper = a.getMapper(BrandsMapper.class);
        List<Brand> brands = mapper.selectAll();
        a.close();
        //4.调用方法
        return brands;
    }
     public void insert(Brand brand)
     {
         SqlSession sqlSession = sqlSessionFactory.openSession();
         BrandsMapper mapper = sqlSession.getMapper(BrandsMapper.class);
         mapper.insert(brand);
         sqlSession.commit();
         sqlSession.close();
     }
     public Brand selectById(int id)
     {
         SqlSession sqlSession = sqlSessionFactory.openSession();
         BrandsMapper mapper = sqlSession.getMapper(BrandsMapper.class);
         Brand brand=mapper.selectById(id);
         sqlSession.close();
         return brand;
     }
     public void update(Brand brand)
     {
         SqlSession sqlSession = sqlSessionFactory.openSession();
         BrandsMapper mapper = sqlSession.getMapper(BrandsMapper.class);
         mapper.update(brand);
         sqlSession.commit();
         sqlSession.close();
     }
     public void delete(int id)
     {
         SqlSession sqlSession = sqlSessionFactory.openSession();
         BrandsMapper mapper = sqlSession.getMapper(BrandsMapper.class);
         mapper.delete(id);
         sqlSession.commit();
         sqlSession.close();
     }
}
